/*
https://leetcode-cn.com/problems/fraction-to-recurring-decimal/solution/fen-shu-dao-xiao-shu-by-leetcode-solutio-tqdw/
 */
import java.util.HashMap;
import java.util.Map;

public class Solution166 {
    public String fractionToDecimal(int numerator, int denominator) {
        StringBuilder ans=new StringBuilder();
        boolean flag=true;
        long humerator;
        long henominator;
        humerator=numerator;
        henominator=denominator;
        if (humerator%henominator==0){
            return String.valueOf(humerator/henominator);
        }
        if (humerator==0){
            return "0";
        }
        if (humerator<0) {
            humerator=-humerator;
            flag=!flag;
        }
        if (henominator<0) {
            henominator=-henominator;
            flag=!flag;
        }
        if (flag==false){
            ans.append('-');
        }
        long t=humerator/henominator;
        humerator=humerator%henominator;
        ans.append(t);
        ans.append('.');
        Map<Long,Integer> map=new HashMap<>();
        long[] r=new long[1000];
        int point=0;
        while (true){
            humerator=humerator*10;
            if (map.containsKey(humerator)){
                int p=map.get(humerator);
                for (int i=1;i<p;i++){
                    ans.append(r[i]);
                }
                ans.append('(');
                for (int i=p;i<=point;i++){
                    ans.append(r[i]);
                }
                ans.append(')');
                return ans.toString();
            }else {
                point++;
                r[point]=humerator/henominator;
                map.put(humerator,point);
                humerator=humerator%henominator;
                if (humerator==0){
                    for (int i=1;i<=point;i++){
                        ans.append(r[i]);
                    }
                    return ans.toString();
                }
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution166().fractionToDecimal(22,7));
    }
}
